package com.soft.servlet;

import com.soft.entity.UserInfo;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserRegisterServlet",urlPatterns = "/UserRegister")
public class UserRegisterServlet extends HttpServlet {

    private Domain domain;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("gbk");

        domain=new Domain();

        String userId = null;
        if (request.getParameter("member.username") != null)
            userId = request.getParameter("member.username");
        String userName = null;
        if (request.getParameter("member.realname") != null)
            userName = request.getParameter("member.realname");
        String address = null;
        if (request.getParameter("member.city") != null)
            address = request.getParameter("member.city");
        String postcode = null;
        if (request.getParameter("member.postcode") != null)
            postcode = request.getParameter("member.postcode");
        String cardNum = null;
        if (request.getParameter("member.cardno") != null)
            cardNum = request.getParameter("member.cardno");
        String IdType = null;
        if (request.getParameter("member.cardtype") != null)
            IdType = request.getParameter("member.cardtype");
        String telephone = null;
        if (request.getParameter("member.tel") != null)
            telephone = request.getParameter("member.tel");
        String email = null;
        if (request.getParameter("member.email") != null)
            email = request.getParameter("member.email");

        UserInfo ui = domain.getUserInfo(userId);
        if(ui.getId()==null) {
            //UserInfo ui = new UserInfo();
            ui.setId(userId);
            ui.setName(userName);
            ui.setLevel("1");
            ui.setPassword("000000");
            ui.setAddress(address);
            ui.setPostalCode(postcode);
            ui.setIdNumber(cardNum);
            ui.setIdType(IdType);
            ui.setTelephone(telephone);
            ui.setEmail(email);
            ui.setStatus("1");
            if(domain.registerUserInfo(ui)){
                request.getSession().setAttribute("flag", 13);
                response.sendRedirect("/denglu.jsp");
            }
            else{
                request.getSession().setAttribute("flag", 14);
                response.sendRedirect("/register.jsp");
            }

        }
        else{
            request.getSession().setAttribute("flag", 12);
            response.sendRedirect("/register.jsp");
        }
    }
}



